# 问题1：__age表示私有属性，但是依然可以访问
# 定义person类
# class Person():
#
#     # 构造函数，用于为成员变量赋初始值
#     def __init__(self, name, age, height):
#         self.name = name
#         self.__age = age
#         self.height = height
#
#     def set_age(self, age):
#         if age < 0 or age > 150:
#             raise Exception("非法入参，年龄不合法")
#         self.__age = age
#
#     def introduce_self(self):
#         print("hello,my name is %s,my age is %d and i'm %d height" % (self.name, self.__age, self.height))
#
#
# person = Person("cendy", 25, 175)
# person.__age = -1
# print("新修改了年龄:", person.__age)
# person.introduce_self()
# print(person.__dict__)

# 问题2：在python中，仅属性或方法前面加上__时，表示私有；如果后面再加上__，此时含义就发生改变，变成了普通的属性或方法
# 定义person类
# class Person():
#
#     # 构造函数，用于为成员变量赋初始值
#     def __init__(self, name, age, height):
#         self.name = name
#         self.__age__ = age
#         self.height = height
#
#     def set_age(self, age):
#         if age < 0 or age > 150:
#             raise Exception("非法入参，年龄不合法")
#         self.__age = age
#
#     def introduce_self(self):
#         print("hello,my name is %s,my age is %d and i'm %d height" % (self.name, self.__age__, self.height))
#
#
# person = Person("cendy", 25, 175)
# person.__age__ = -1
# print("新修改了年龄:", person.__age__)
# person.introduce_self()
# print(person.__dict__)

# 问题3：通过问题1，我们看出对象的__dict__打印出来的结果，多了个_Person__age，其值刚好对应私有属性的值，因此，可以如下访问
# 定义person类
class Person():

    # 构造函数，用于为成员变量赋初始值
    def __init__(self, name, age, height):
        self.name = name
        self.__age = age
        self.height = height

    def set_age(self, age):
        if age < 0 or age > 150:
            raise Exception("非法入参，年龄不合法")
        self.__age = age

    def introduce_self(self):
        print("hello,my name is %s,my age is %d and i'm %d height" % (self.name, self.__age, self.height))


person = Person("cendy", 25, 175)
# 打破访问限制
print(person.__dict__)
print(person._Person__age)
person.introduce_self()


